package com.tycmc.signelectroniccontracts.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.TextView;

import com.tycmc.signelectroniccontracts.tools.other.TextShow;

/**
 * Created by Spinel on 15/9/15.
 * Edited by Spinel on 15/11/4.
 */
@SuppressLint("AppCompatCustomView")
public class CustomEditText extends EditText {
    private Drawable dLeft;
    private Drawable dRight;
    private Rect rBounds;
    private OnListenPaste paste;
    private OnTouchListener onTouchListener;
    private RightOnClickLister rightOnclickLister;


    public CustomEditText(Context paramContext) {
        super(paramContext);
        initEditText();
    }

    public CustomEditText(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        initEditText();
    }

    public CustomEditText(Context paramContext,
                          AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);

        initEditText();
    }


    private void initEditText() {
        setEditTextDrawable();
        addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable paramEditable) {
//                String trim = paramEditable.toString().trim();
//                for (int i = 0; i < strNoList.size(); i++) {
//                    if (trim.contains(strNoList.get(i))) {
//                        String str = "*";
//                        for (int j = 1; j < strNoList.get(i).length(); j++) {
//                            str = str + "*";
//                        }
//                        String replaceAll = trim.replaceAll(strNoList.get(i), str);
//                        CustomEditText.this.setText(replaceAll);
//                        setSelection(replaceAll.length());
//                    }
//
//                }
            }

            @Override
            public void beforeTextChanged(CharSequence paramCharSequence,
                                          int paramInt1, int paramInt2, int paramInt3) {
            }

            @Override
            public void onTextChanged(CharSequence paramCharSequence,
                                      int paramInt1, int paramInt2, int paramInt3) {
                CustomEditText.this.setEditTextDrawable();

            }
        });
//        setProhibitEmoji(this);
    }

    public void setProhibitEmoji(EditText et) {
        InputFilter[] filters = {getInputFilterProhibitEmoji()};
        et.setFilters(filters);
    }

    private InputFilter getInputFilterProhibitEmoji() {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                                       Spanned dest, int dstart, int dend) {
                StringBuffer buffer = new StringBuffer();
                for (int i = start; i < end; i++) {
                    char codePoint = source.charAt(i);
                    if (!getIsEmoji(codePoint)) {
                        buffer.append(codePoint);
                    } else {
                        i++;
                        continue;
                    }
                }
                if (source instanceof Spanned) {
                    SpannableString sp = new SpannableString(buffer);
                    TextUtils.copySpansFrom((Spanned) source, start, end, null,
                            sp, 0);
                    return sp;
                } else {
                    return buffer;
                }
            }
        };

        return filter;
    }

    private boolean getIsEmoji(char codePoint) {
        if ((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)))
            return false;
        return true;
    }

    public void setEditTextDrawable() {
        if (getText().toString().length() == 0) {
            setCompoundDrawables(this.dLeft, null, null, null);
        } else {
            setCompoundDrawables(this.dLeft, null, this.dRight, null);
        }
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        boolean b = super.onTextContextMenuItem(id);
        if (id == android.R.id.paste) {
            if (paste != null) {
                paste.onListPaste(this, this.getText().toString());
            }
            this.setSelection(this.getText().toString().length());
        }
        return b;
    }

    public void setOnListenPaste(OnListenPaste paste) {
        this.paste = paste;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.dLeft = null;
        this.dRight = null;
        this.rBounds = null;

    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent paramMotionEvent) {

        if ((this.dRight != null) && (paramMotionEvent.getAction() == 1)) {
            this.rBounds = this.dRight.getBounds();
            int i = (int) paramMotionEvent.getRawX(); // to screen
            // int i = (int) paramMotionEvent.getX(); // to bound
            if (i > getRight() - 3 * this.rBounds.width()) {
//                setText("");
                paramMotionEvent.setAction(MotionEvent.ACTION_CANCEL);
                if (rightOnclickLister != null) {
                    rightOnclickLister.onClick(this);
                } else {
                    setText("");
                }
            }
        }
        return super.onTouchEvent(paramMotionEvent);
    }

    public void setRightOnclickLister(RightOnClickLister rightOnclickLister) {
        this.rightOnclickLister = rightOnclickLister;
    }

    public interface RightOnClickLister {
        void onClick(TextView view);
    }

    @Override
    public void setCompoundDrawables(Drawable paramDrawable1,
                                     Drawable paramDrawable2, Drawable paramDrawable3,
                                     Drawable paramDrawable4) {
        if (paramDrawable1 != null) {
            this.dLeft = paramDrawable1;
        }
        if (paramDrawable3 != null) {
            this.dRight = paramDrawable3;
        }
        super.setCompoundDrawables(paramDrawable1, paramDrawable2,
                paramDrawable3, paramDrawable4);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //让父类不不拦截自己的触摸事件
        if (!TextShow.isEmpty(getText().toString().trim())) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return super.dispatchTouchEvent(ev);
    }

    public interface OnListenPaste {
        void onListPaste(EditText et, String str);
    }

}